package com.huanxing.cloud.auth.handler;

import com.huanxing.cloud.common.security.hander.AbstractAuthenticationFailureEvenHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/** @Author 认证失败处理 @Date 2022/1/27 9:57 */
@Slf4j
@Component
@AllArgsConstructor
public class AuthenticationFailureEvenHandler extends AbstractAuthenticationFailureEvenHandler {
  @Override
  public void handle(
      AuthenticationException authenticationException, Authentication authentication) {
    log.info(
        "用户：{} 登录失败，异常：{}",
        authentication.getPrincipal(),
        authenticationException.getLocalizedMessage());
    HttpServletRequest request =
        ((ServletRequestAttributes)
                Objects.requireNonNull(RequestContextHolder.getRequestAttributes()))
            .getRequest();
  }
}
